|
1
|
|
|
const mqtt = require('async-mqtt') |
|
2
|
|
|
const configuration = require('./configuration') |
|
3
|
|
|
const validate = require('./validate') |
|
4
|
|
|
|
|
5
|
|
|
module.exports = (input, callback) => { |
|
6
|
|
|
|
|
7
|
|
|
let error = null |
|
8
|
|
|
let output = {} |
|
9
|
|
|
|
|
10
|
|
|
validate.sourceConfiguration(input, (validatedInput, thrownError) => { |
|
11
|
|
|
input = validatedInput |
|
12
|
|
|
error = thrownError |
|
13
|
|
|
}) |
|
14
|
|
|
validate.paramConfiguration(input, (validatedInput, thrownError) => { |
|
15
|
|
|
input = validatedInput |
|
16
|
|
|
error = thrownError |
|
17
|
|
|
}) |
|
18
|
|
|
|
|
19
|
|
|
// Send MQTT |
|
20
|
|
|
if (!error) { |
|
21
|
|
|
let configurationMqtt = configuration.mqtt(input) |
|
22
|
|
|
let client = mqtt.connect(input.source.url, configurationMqtt) |
|
23
|
|
|
|
|
24
|
|
|
input.params.qos = 0 |
|
25
|
|
|
const sendMessage = async () => { |
|
26
|
|
|
try { |
|
27
|
|
|
await client.publish( |
|
28
|
|
|
input.params.topic, |
|
29
|
|
|
input.params.payload.toString() |
|
30
|
|
|
) |
|
31
|
|
|
output = { |
|
32
|
|
|
'version': {'message': input.params.payload.toString()}, |
|
33
|
|
|
'metadata': [ |
|
34
|
|
|
{'name': 'ATC_EXTERNAL_URL', 'value': process.env.ATC_EXTERNAL_URL || 'not set'}, |
|
35
|
|
|
{'name': 'BUILD_ID', 'value': process.env.BUILD_ID || 'not set'}, |
|
36
|
|
|
{'name': 'BUILD_NAME', 'value': process.env.BUILD_NAME || 'not set'}, |
|
37
|
|
|
{'name': 'BUILD_PIPELINE_NAME', 'value': process.env.BUILD_PIPELINE_NAME || 'not set'}, |
|
38
|
|
|
{'name': 'BUILD_TEAM_NAME', 'value': process.env.BUILD_TEAM_NAME || 'not set'}, |
|
39
|
|
|
{'name': 'source.url', 'value': input.source.url || 'not set'}, |
|
40
|
|
|
{'name': 'source.port', 'value': input.source.port.toString() || 'not set'}, |
|
41
|
|
|
{'name': 'params.payload', 'value': input.params.payload || 'not set'}, |
|
42
|
|
|
{'name': 'params.topic', 'value': input.params.topic || 'not set'}, |
|
43
|
|
|
{'name': 'params.qos', 'value': input.params.qos.toString() || 'not set'} |
|
44
|
|
|
] |
|
45
|
|
|
} |
|
46
|
|
|
await client.end() |
|
47
|
|
|
callback(error, output) |
|
48
|
|
|
} catch (e) { |
|
49
|
|
|
callback(e.stack, {}) |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
client.on('connect', sendMessage) |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|